package cn.cyber.wxfire.controller;

import cn.cyber.wxfire.pojo.Student;
import cn.cyber.wxfire.service.SendSessionService;
import cn.cyber.wxfire.service.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentServer studentServer;

    private SendSessionService sendSessionService=new SendSessionService();

    public List<Student> StudentSort(List<Student> sl,int flag){
        if(flag==0){
            Collections.sort(sl, new Comparator<Student>() {
                @Override
                public int compare(Student p1, Student p2) {
                    if(Integer.parseInt(p1.getNum())>Integer.parseInt(p2.getNum()))
                    {
                        return -1;
                    }
                    else{
                        return 1;
                    }
                }
            });
        }
       else
        {
            Collections.sort(sl, new Comparator<Student>() {
                @Override
                public int compare(Student p1, Student p2) {
                    if(Integer.parseInt(p1.getNum())>Integer.parseInt(p2.getNum()))
                    {
                        return 1;
                    }
                    else{
                        return -1;
                    }
                }
            });
        }
        return sl;
    }



    //课程人员
    @RequestMapping("/stu/all/{num}/{flag}")
    public String queryStudentList(@PathVariable("flag") int flag,@PathVariable("num") String num,Model model,HttpSession session)
    {
        List<Student> studentsList=studentServer.studentCourseList(num);
        studentsList=StudentSort(studentsList,flag);
        model.addAttribute("stus",studentsList);
        model.addAttribute("num",num);
        sendSessionService.sendSession(model, session);
        return "stu/list";
    }

    @RequestMapping(value="/add",method= RequestMethod.POST)
    public String addStudent(Student student,Model model,HttpSession session)
    {
        String cNum=(String) session.getAttribute("cnum");
        student.setCourse(cNum);
        studentServer.addStudent(student);
        studentServer.addSC(student.getNum(),cNum);
        List<Student> studentsList=studentServer.studentCourseList(cNum);
        studentsList=StudentSort(studentsList,1);
        model.addAttribute("stus",studentsList);
        sendSessionService.sendSession(model, session);
        return "teacher/list";
    }

    @GetMapping("/stu/update/{num}")
    public String update(@PathVariable("num") String num,Model model,HttpSession session)
    {
        List<Student> studentsList=studentServer.queryStudentList();
        for (Student stu:studentsList) {
            if(stu.getNum().equals(num))
            {
                sendSessionService.sendSession(model, session);
                model.addAttribute("stu_u",stu);
                break;
            }

        }
        return "/stu/update";
    }

    @GetMapping("/stu/privateUpdate/{num}")
    public String privateUpdate(@PathVariable("num") String num,Model model,HttpSession session)
    {
        List<Student> studentsList=studentServer.queryStudentList();
        for (Student stu:studentsList) {
            if(stu.getNum().equals(num))
            {

                model.addAttribute("stu_u",stu);
                sendSessionService.sendSession(model, session);
                break;
            }

        }
        return "/stu/privateUpdate";
    }

    @RequestMapping(value = "/stu/privateUpdate/finish/{num}",method = RequestMethod.POST)
    public String privateUpdateStudent(Student student,
                                       HttpServletResponse response,
                                       @PathVariable("num") String num,
                                       @RequestParam("OldPassword") String OldPassword,
                                       @RequestParam("NewPassword") String NewPassword,
                                       @RequestParam("SurePassword") String SurePassword,
                                       Model model,HttpSession session)
    {

        response.setCharacterEncoding("GBK");
        List<Student> studentsList=studentServer.queryStudentList();
        String password="";
        for (Student stu:studentsList) {
            if(stu.getNum().equals(num))
            {
                password=stu.getPassword();
                break;
            }
        }
        if(password.equals(OldPassword)){
            if(NewPassword.equals(SurePassword)){
                student.setNum(num);
                student.setPassword(NewPassword);
                studentServer.privateUpdateStudent(student);
                studentsList=studentServer.queryStudentList();
                studentsList=StudentSort(studentsList,1);
                model.addAttribute("stus",studentsList);
                sendSessionService.sendSession(model, session);
                return "stu/list";
            }
            else{

//                model.addAttribute("msg2","新密码与确认密码不一样");
                PrintWriter out = null;
                try {
                    out = response.getWriter();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out.print("<script>alert('新密码与确认密码不一样'); window.location.href='http://localhost:80/stu/privateUpdate/"+num+"' </script>");
                out.flush();
                out.close();
                sendSessionService.sendSession(model, session);
                return "redirect:../stu/privateUpdate/"+num;
            }
        }
        else{
//            model.addAttribute("msg2","原密码不正确");

            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("<script>alert('原密码不正确'); window.location.href='http://localhost:80/stu/privateUpdate/"+num+"' </script>");
            out.flush();
            out.close();
            sendSessionService.sendSession(model, session);
            return "redirect:../stu/privateUpdate/"+num;
        }



    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updateStudent(Student student,Model model,HttpSession session)
    {

        studentServer.updateStudent(student);
        List<Student> studentsList=studentServer.studentCourseList((String) session.getAttribute("cnum"));
        studentsList=StudentSort(studentsList,1);
        model.addAttribute("stus",studentsList);
        sendSessionService.sendSession(model, session);
        return "teacher/list";
    }

    @GetMapping("/stu/delete/{num}")
    public String deleteStudent(@PathVariable("num") String num,Model model,HttpSession session)
    {
        studentServer.deleteStudent(num);
        List<Student> studentsList=studentServer.studentCourseList((String) session.getAttribute("cnum"));
        studentsList=StudentSort(studentsList,1);
        model.addAttribute("stus",studentsList);
        sendSessionService.sendSession(model, session);
        return "teacher/list";
    }

    @GetMapping("/find/{num}")
    public String findStudent(@RequestParam("flag") String flag, @PathVariable("num") String num,Model model,HttpServletResponse response,HttpSession session)
    {
        response.setCharacterEncoding("UTF-8");
        List<Student> studentsList=studentServer.findStudent(flag,num);
        System.out.println(studentsList);
        if(studentsList.isEmpty()){
            System.out.println(studentsList);
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("<script>alert('未查询到该人物'); window.location.href='http://localhost/stu/all/"+num+"/1'</script>");
            out.flush();
            out.close();

        }

        studentsList=StudentSort(studentsList,1);
        model.addAttribute("stus",studentsList);
        sendSessionService.sendSession(model, session);
        if(session.getAttribute("identity").equals("student"))
            return "stu/list";
        else
            return "teacher/list";
    }




}
